package com.simple.car.loader.server.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.simple.car.loader.server.annotation.Function;
import com.simple.car.loader.server.annotation.Init;
import com.simple.car.loader.server.entity.RunningInfo;
import com.simple.car.loader.server.tools.DatabaseHelper;

import java.sql.SQLException;
import java.util.List;

@Function
public class RunningInfoDao {

    private Dao<RunningInfo, Integer> dao;

    @Init
    public void init(Context context) {
        try {
            dao = DatabaseHelper.getInstance(context).getDao(RunningInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int insert(RunningInfo runningInfo) {
        try {
            return dao.create(runningInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int updateById(RunningInfo runningInfo){
        try {
            return dao.update(runningInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteById(int id) {
        try {
            return dao.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<RunningInfo> listByStatue(int statue) {
        try {
            return dao.queryBuilder().where().eq("statue", statue).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long countByStatue(int statue) {
        try {
            return dao.queryBuilder().where().eq("statue", statue).countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public long count() {
        try {
            return dao.countOf();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
